package com.ujson.chat.common.handler;

import com.ujson.chat.common.proto.MessageProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Objects;

public abstract class AbstractHandler extends ChannelInboundHandlerAdapter {

    public abstract MessageProto.MessageType getMessageType();

    public boolean messageTypeCheck(ChannelHandlerContext ctx, Object msg) throws Exception {
        MessageProto.Message message = (MessageProto.Message) msg;
        if (Objects.isNull(msg)
                || !(msg instanceof MessageProto.Message)
                || (message = (MessageProto.Message) msg).getType() != getMessageType()) {
            super.channelRead(ctx, msg);
            return false;
        }
        return true;
    }
}
